#include <stdio.h>
#include <stdlib.h>
#include "string.h"
typedef struct student
{
  char name[128];
  int age;
} student;
// dest 需要的大小
// src 原来的大小
student *my_realloc(student *ptr, size_t dest, size_t src)
{
  if (dest == 0) // 需要的大小为零
  {
    free(ptr);
    return NULL;
  }
  if (dest < src) // 需要的大小比原来的大小小
  {
    student *last = (student *)malloc(dest * sizeof(student));
    for (size_t i = 0; i < dest; i++)
    {
      last[i] = ptr[i];
    }
    return last;
  }
  if (ptr == NULL) // 原来的为NULL
  {
    printf("原来创建空间失败\n");
    student *last = (student *)malloc(dest * sizeof(student));
    return last;
  }
  if (dest >= src) // 需要的比原来的大
  {
    student *last = (student *)malloc(dest * sizeof(student));
    for (size_t i = 0; i < dest; i++)
    {
      last[i] = ptr[i];
    }
    return last;
  }
}
int main(int argc, char const *argv[])
{

  student *stu = (student *)malloc(2 * sizeof(student));
  my_realloc(stu, 20, 2);
  stu[1].age = 18;
  strcpy(stu[1].name, "yxx");
  stu[0].age = 16;
  strcpy(stu[0].name, "zt");
  for (int i = 0; i < 20; i++)
  {
    printf("名字：%s\t|年龄：%d\t|%d\n", stu[i].name, stu[i].age, i + 1);
  }
  return 0;
}
